package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.NewsModel;
import com.wanxi.model.NewsTypeModel;
import com.wanxi.service.NewsService;
import com.wanxi.service.NewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Li
 * @Date 2023/7/7 21:19
 */
@RestController
@Api(tags = "新闻管理", value = "用户Open api 入口")
@RequestMapping("/back/news")
public class NewsController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private NewsTypeService newsTypeService;

    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询所有新闻信息", response = NewsModel.class, httpMethod = "GET")
    public ResultDto findAll(NewsModel newsModel) {
        Page<NewsModel> page = new Page<>(newsModel.getPage(), newsModel.getLimit());
        QueryWrapper<NewsModel> queryWrappers = query(newsModel);
        IPage<NewsModel> iPage = newsService.page(page, queryWrappers);
        for (NewsModel newsModel1 : iPage.getRecords()) {
            if (newsModel1.getNewsId() != null) {
                //新闻类型service进行查库
                NewsTypeModel newsTypeModel = newsTypeService.getById(newsModel1.getNewsId());
                newsModel1.setNewsName(newsTypeModel.getNewType());
            }
        }
        return ResultDto.builder()
                .code(0)
                .data(iPage.getRecords())
                .count((int) iPage.getTotal())
                .build();
    }

    @PostMapping("/addNews")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增新闻", response = NewsModel.class, httpMethod = "POST")
    public ResultDto addNews(NewsModel newsModel) {
        newsService.save(newsModel);
        return ResultDto.builder()
                .code(0)
                .msg("添加成功！")
                .build();
    }

    @PostMapping("/updateNews")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改新闻", response = NewsModel.class, httpMethod = "GET")
    public ResultDto updateNews(NewsModel newsModel) {
        newsService.updateById(newsModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功！")
                .build();
    }

    @PostMapping("deleteNews")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除新闻", response = NewsModel.class, httpMethod = "POST")
    public ResultDto deleteNews(long id) {
        newsService.removeById(id);
        return ResultDto.builder().code(0).msg("删除成功").build();
    }

    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询新闻信息", response = NewsModel.class, httpMethod = "GET")
    public ResultDto findById(NewsModel newsModel) {
        NewsModel news = newsService.getById(newsModel.getId());
        return ResultDto.builder().code(0).data(news).build();
    }


    private QueryWrapper<NewsModel> query(NewsModel newsModel) {
        QueryWrapper<NewsModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(newsModel.getTitle() == null), "title", newsModel.getTitle());
        queryWrapper.like(!(newsModel.getContent() == null), "content", newsModel.getContent());
        if (newsModel.getStartTime() != null && "".equals(newsModel.getEndTime())) {
            queryWrapper.ge("time", newsModel.getStartTime());
        } else if ("".equals(newsModel.getStartTime()) && newsModel.getEndTime() != null) {
            queryWrapper.le("time", newsModel.getEndTime());
        } else {
            queryWrapper.between(newsModel.getStartTime() != null && newsModel.getEndTime() != null, "time", newsModel.getStartTime(), newsModel.getEndTime());
        }

        queryWrapper.eq(newsModel.getEnable() != null && newsModel.getEnable() != -1, "enable", newsModel.getEnable());
        return queryWrapper;
    }
}
